﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
using System;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
namespace Microsoft.MCSUK.SPSiteDirectory2010
{
    [Guid("76EB44CE-98C2-4689-8B9A-90B0366570E8")]
    public class GlobalSolutionSettings : SPPersistedObject
    {
        [Persisted]
        public Guid SiteDirectorySiteGuid;
        [Persisted]
        public Guid SiteDirectoryWebGuid;
        [Persisted]
        public string SiteDirectoryURL;
        [Persisted]
        public Int32 DeleteBrokenListingNumberScans;
        [Persisted]
        public Boolean IsDeleteBrokenListingJobConfigured;
        [Persisted]
        public Guid DeleteJobDefinitionGUID;
        public GlobalSolutionSettings() { }
        public GlobalSolutionSettings(string name, SPPersistedObject parent, Guid id) : base(name, parent, id) { }

        protected override bool HasAdditionalUpdateAccess()
        {
            return true;
        }

        public GlobalSolutionSettings GetOrCreateGlobalSolutionSettings(SPPersistedObject farm)
        {
            GlobalSolutionSettings globalSolutionSettings = farm.GetChild<GlobalSolutionSettings>(GlobalConstants.SolutionName);
            try
            {
                if (globalSolutionSettings == null)
                {
                    SPPersistedObject parent = farm;
                    globalSolutionSettings = new GlobalSolutionSettings(GlobalConstants.SolutionName, parent, Guid.NewGuid());
                    globalSolutionSettings.Update();
                }
            }
            catch (Exception ex)
            {
                GlobalMethods globalMethods = new GlobalMethods();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
            return globalSolutionSettings;
        }

    }
}
